package com.at.atapp;


import java.util.HashMap;
import java.util.Map;

import com.at.appmodel.APPPreferences;
import com.at.atapp.R;
import com.at.atapp.web.MainScreenWebClient;
import com.at.atapp.web.RestClient;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity {
private Intent svc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		WebView webview = (WebView)findViewById(R.id.webViewmain);
		// use cookies to remember a logged in status   
		CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(webview.getContext());
	    CookieManager cookieManager = CookieManager.getInstance();
	    cookieManager.setAcceptCookie(true);
	    cookieManager.removeSessionCookie();

	    //webview.getSettings().setDefaultTextEncodingName("utf-8");
	    cookieSyncManager.sync();
 
	 
	    webview.getSettings().setJavaScriptEnabled(true);
 

		
		MainScreenWebClient oMainScreenWebClient	= new MainScreenWebClient();
		RestClient oRestClient = new RestClient(webview);
		
		webview.setWebViewClient(oMainScreenWebClient);
 
		//webview.loadUrl("http://www.benbox.org/benbox/wp-admin/profile.php");
		
		
		oRestClient.webLogin();

		/* Map<String, String> extraHeaders = new HashMap<String, String>();
		  extraHeaders.put("charset", "UTF-8");*/
		  
		 // webview.getSettings().setDefaultTextEncodingName("UTF-8");
		//webview.loadUrl("http://www.benbox.org/benbox/index.php?OperatingSystem=Android");
		APPPreferences	oAppPreferences = new APPPreferences(getBaseContext());
		
		if(!oAppPreferences.isApplicationsFirstRun()){
			
			oAppPreferences.setupAtFirstRun();
			
			oAppPreferences.setApplicationsFirstRun(true);
		}
		
		svc	= new Intent(getBaseContext(),ATAlarmService.class);
		startService(svc);
		
		Button button_alarm	= (Button) findViewById(R.id.button_alarm);
		button_alarm.setOnClickListener(button_alarm_handler);
		
		Button button_time_management	= (Button) findViewById(R.id.button_time_management);
		button_time_management.setOnClickListener(button_time_manage_handler);
		
		Button button_next_turn	= (Button) findViewById(R.id.button_next_turn);
		button_next_turn.setOnClickListener(button_next_turn_handler);
			
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
			
		return true;
	}
	
	 
	View.OnClickListener button_alarm_handler = new View.OnClickListener() {
		  public void onClick(View v) {

              Intent intent = new Intent(MainActivity.this, AlarmManager.class);
              MainActivity.this.startActivity(intent);
		  }
	 };
	 
	View.OnClickListener button_time_manage_handler = new View.OnClickListener() {
		  public void onClick(View v) {

              Intent intent = new Intent(MainActivity.this, TimeManagement.class);
              MainActivity.this.startActivity(intent);
		  }
	 };
	 
	View.OnClickListener button_next_turn_handler = new View.OnClickListener() {
		  public void onClick(View v) {

              Intent intent = new Intent(MainActivity.this, ATTurn.class);
              MainActivity.this.startActivity(intent);
			  //triggerNotification("wsw");
		  }
	 };		 
	 //----------------------------------------------------------------

}
